//
//  XPWelcomeViewController.swift
//  PPKit
//
//  Created by xiaopin on 2019/7/12.
//  Copyright © 2019 scoresdata.net. All rights reserved.
//

import UIKit
import Kingfisher

/// 欢迎页面+广告
class XPWelcomeViewController: XPBaseViewController {
    //MARK: 属性
    @IBOutlet weak var skipBtn: UIButton!
    @IBOutlet weak var adImgView: UIImageView!
    @IBOutlet weak var labAd: UILabel!
    
    /// 是否是通过外链点击进入，默认不是
    var isJumpIn:Bool = false
    var isSkiped:Bool = false
    var isShowAd:Bool = false
    
    /// 当前广告对象
    //var adModel:MTAdModel?
    //MARK: 懒加载
    
    //MARK: 默认基类方法
    override func viewDidLoad() {
        super.viewDidLoad()
        initSubViews()
        loadData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        hideNavigationBar()
        
    }
}

//MARK: 自定义方法
extension XPWelcomeViewController{
    override func initSubViews() {
        super.initSubViews()
        
        if XPApplication.shared.isAgreedPrivacy{
            firebaseLogEvent("WhenStart")
        }
        
        self.skipBtn.cornerRadius = 20
        self.skipBtn.borderColor = .white
        self.skipBtn.isHidden = true
        self.labAd.isHidden = true
        
        NotificationCenter.default.addObserver(forName: .mtBackToWelcome, object: nil, queue: .main) { [weak self] (notify) in
            self?.enterApp()
        }
    }
    
    override func loadData() {
        /*
         1.先判断是否有缓存配置基础信息，如果没有，说明是第一次，获取最新的，然后进入首页
         2.如果有缓存，直接读取缓存信息，后台线程更新最新基础信息缓存
         **/
        loadAd()
    }
    
    func loadAd(){
        /*
        XPNetworkApi.shared.common_fetchAd(position: .startup, count: 1, cache: true) { [weak self] (msg, count, res) in
            let ads = res as! [MTAdModel]
            if ads.count == 0 {
                self?.enterApp()
                return
            }
            self?.adModel = ads.first
            self?.adImgView.loadImage(ads.first?.image)
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self!.jumpToPage))
            self?.adImgView.addGestureRecognizer(tapGesture)
            
            self?.skipBtn.isHidden = false
            //self?.labAd.isHidden = false
            pp_dispatchTimer(timeInterval: 1, repeatCount: 5) {(timer, count) in
                self?.skipBtn.setTitle("跳过(\(count))", for: .normal)
                //未点击跳过且倒计时到0, 非跳转景来，未显示广告
                guard let skiped = self?.isSkiped,!skiped,count == 0, let isJumpIn = self?.isJumpIn, isJumpIn == false, let showad = self?.isShowAd, showad == false else{
                    return
                }
                self?.enterApp()
            }
        } failed: { [weak self] (msg, code) in
            self?.enterApp()
        }*/
    }
    
    func enterApp(){
        //pp_keyWindow()?.rootViewController = XPApplication.shared.isFirstLaunch ? XPGuidePageViewController() : XPTabBarController()
    }
}

//MARK: 事件通知
extension XPWelcomeViewController{
    @IBAction func skipClick(_ sender: Any) {
        isSkiped = true
        enterApp()
    }
    
    func jumpToPage(){
        //guard let model = adModel else { return }
        /*
         广告点击跳转后，标志显示广告的状态true,这样不会自动跳转到首页
         **/
        isShowAd = true
        
//        switch model.object_type {
//        case 3:
//            let vc = MTWebViewController()
//            vc.pushSource = .fromWelcome
//            vc.title = model.core_name
//            vc.webUrl = adModel?.url;
//            push(vc)
//        default:
//            break
//        }
    }
}
